/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	libitum 
 * 
 * <p>描   述  :   BaseManager.java for com.pengtu.service.business    </p>
 * 
 * <p>最后修改 : $: 2011-7-6-下午02:37:04 v 1.0.0	 libitum   $     </p>
 * 
 */

package com.pengtu.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.model.ActivityCoordinates;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.entity.app.User;
import com.pengtu.service.JbpmTemplateManager;
import com.pengtu.service.custom.CountersignInfo;

/**
 * 
 * BaseManager
 * 
 * 2011-7-6 下午02:37:04
 * 
 * @version 1.0.0
 * 
 */
@Component
@Transactional
public class BaseManager {

	private JbpmTemplateManager jbpmTemplateManager;

	@Autowired
	public void setJbpmTemplateManager(JbpmTemplateManager jbpmTemplateManager) {
		this.jbpmTemplateManager = jbpmTemplateManager;
	}

	/**
	 * 启动流程 start: 适用:
	 * 
	 * @param id
	 * @param deploymentId
	 * @param key
	 * @param variables
	 * @param user
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public void start(String id, String deploymentId, String key,
			Map<String, Object> variables) {
		jbpmTemplateManager.startProcess(deploymentId, variables, key, id);
	}

	/**
	 * 设置变量 setVariables: 适用:
	 * 
	 * @param taskId
	 * @param variables
	 * @exception
	 * @since 1.0.0
	 */
	public void setVariables(String taskId, Map<String, Object> variables) {
		jbpmTemplateManager.setVariables(taskId, variables);
	}

	/**
	 * 接受任务 takeTask: 适用:
	 * 
	 * @param taskId
	 * @param user
	 * @exception
	 * @since 1.0.0
	 */
	public void takeTask(String taskId, User user) {
		jbpmTemplateManager.takeTask(taskId, user.getUserName());
	}

	/**
	 * 提交任务 completeTask: 适用:
	 * 
	 * @param taskId
	 * @param outcome
	 * @param variables
	 * @exception
	 * @since 1.0.0
	 */
	public void completeTask(String taskId, String outcome,
			Map<String, Object> variables) {
		if (StringUtils.isEmpty(outcome)) {
			jbpmTemplateManager.completeTask(taskId, variables);
		} else {
			jbpmTemplateManager.completeTask(taskId, outcome, variables);
		}
	}

	/**
	 * 查询待接任务列表 findGroupTasks: 适用:
	 * 
	 * @param user
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public List<Task> findGroupTasks(User user) {
		return jbpmTemplateManager.findGroupTasks(user);
	}

	/**
	 * 获取指定用户的任务 findPersonalTasks: 适用:
	 * 
	 * @param user
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public List<Task> findPersonalTasks(User user) {
		return jbpmTemplateManager.findPersonalTasks(user.getUserName());
	}

	/**
	 * 根据任务id获取指定变量值 getVariableByTaskId: 适用:
	 * 
	 * @param taskId
	 * @param variableName
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public Object getVariableByTaskId(String taskId, String variableName) {
		return jbpmTemplateManager.getVariableByTaskId(taskId, variableName);
	}

	/**
	 * 通过业务ID获得坐标 getActivityNames: 适用:
	 * 
	 * @param id
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public ActivityCoordinates getActivityNames(String id) {
		ProcessInstance processInstance = jbpmTemplateManager
				.getProcessInstance(id);
		ActivityCoordinates ac = jbpmTemplateManager
				.getAllActivityNames(processInstance);
		return ac;
	}

	/**
	 * 判断是否是待办任务 isPersonalTasks: 适用:业务受理
	 * 
	 * @param user
	 * @param id
	 * @param key
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String isPersonalTasks(User user, String id, String key) {
		List<Task> tasks1 = jbpmTemplateManager.findGroupTasks(user);
		List<Task> tasks2 = jbpmTemplateManager.findPersonalTasks(user
				.getUserName());
		if (tasks1.size() != 0 && null != tasks1) {
			for (Task task : tasks1) {
				String taskId = task.getId();
				String fid = (String) jbpmTemplateManager.getVariableByTaskId(
						taskId, key);
				if (StringUtils.isNotEmpty(fid) && fid.equals(id)) {
					return taskId;
				}
			}
		}
		if (tasks2.size() != 0 && null != tasks2) {
			for (Task task : tasks2) {
				String taskId = task.getId();
				String fid = (String) jbpmTemplateManager.getVariableByTaskId(
						taskId, key);
				if (StringUtils.isNotEmpty(fid) && fid.equals(id)) {
					return taskId;
				}
			}
		}
		return null;
	}

	/**
	 * 根据业务ID查询任务ID getTaskById: 适用:
	 * 
	 * @param id
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public Task getTaskById(String id) {
		return jbpmTemplateManager.getTaskById(id);
	}

	public Task getTask(String taskId) {
		return jbpmTemplateManager.getTask(taskId);
	}

	/**
	 * 删除任务 deleteTask: 适用:
	 * 
	 * @param taskId
	 * @exception
	 * @since 1.0.0
	 */
	public void deleteTask(String taskId) {
		jbpmTemplateManager.deleteTask(taskId);
	}

	public void deleteTask(String taskId, String reason) {
		jbpmTemplateManager.deleteTask(taskId, reason);
	}

	public void deleteTaskCascade(String taskId) {
		jbpmTemplateManager.deleteTask(taskId);
	}

	/**
	 * 会签提交 isCounterSign: 适用:
	 * 
	 * @param joinsignTaskId
	 * @param variables
	 * @param conclusion
	 * @param user
	 * @param flag
	 * @exception
	 * @since 1.0.0
	 */
	public void isCounterSign(final String joinsignTaskId,
			final Map<String, Object> variables, final String conclusion,
			final User user, final int flag) {
		if (flag == 0) {
			jbpmTemplateManager.isCounterSign(joinsignTaskId, variables,
					conclusion, user);
			return;
		} else {
			jbpmTemplateManager.isCounterSign(joinsignTaskId, variables,
					conclusion, user, flag);
		}
	}

	/**
	 * 判断是否是领导 isLeader: 适用:
	 * 
	 * @param taskId
	 * @param user
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public boolean isLeader(String taskId, User user) {
		return jbpmTemplateManager.isLeader(taskId, user);
	}

	/**
	 * 得到会签处理帮助类 getCountersignInfo: 适用:
	 * 
	 * @param subTaskId
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public CountersignInfo getCountersignInfo(String subTaskId) {
		return jbpmTemplateManager.getCountersignInfo(subTaskId);
	}
}
